package com.zx.idc.backend.gui.tlog;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.common.BasicBaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.ds.tlog.entity.TLogSecurity;
import com.zx.idc.ds.tlog.service.ITLogSecurityService;
import com.zx.idc.ds.tlog.vo.TLogSecurityVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 安全日志 前端控制器
 * </p>
 *
 * @author 陈冬
 * @since 2019-04-09
 */
@RestController
@RequestMapping("/api/tlogSecurity")
public class TlogSecurityController extends BasicBaseApi<ITLogSecurityService, TLogSecurity> {

    @GetMapping("/query-page")
    @RequiresPermissions("transaction-log:t-log-security")
    public Result<Page<TLogSecurityVo>> page(Page<TLogSecurityVo> page, TLogSecurityVo tlogSecurityVo) {
        return success(service.pageByParam(page, tlogSecurityVo));
    }

    /**
     * 根据ID获取记录
     */
    @GetMapping("get/{id}")
    @RequiresPermissions("transaction-log:t-log-security")
    public Result<TLogSecurityVo> get(@PathVariable("id") Integer id) {
        return success(service.get(id));
    }

    @GetMapping("/get/user-id/{userId}")
    @RequiresPermissions("transaction-log:t-log-security")
    public Result<TLogSecurity> getByUserId(@PathVariable("userId") Integer userId) {
        return success(
            service.selectOne(
                new EntityWrapper<>(
                    new TLogSecurity()
                        .setUserID(userId)
                        .setEventType(TLogSecurity.LOGOUT)
                )
                    .orderBy("actTime", false)
                    .last("limit 1")
            )
        );
    }
}

